package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.InformType;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgInformUserMapper;
import com.ygqh.baby.model.UserInformModel;
import com.ygqh.baby.po.YgInformUser;
import com.ygqh.baby.po.YgInformUserExample;
import com.ygqh.baby.service.YgInformUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.List;

/**
 * @author Ss
 */
@Service
public class YgInformUserServiceImpl implements YgInformUserService{
	
	@Autowired
	private YgInformUserMapper ygInformUserMapper;
	
	@Override
	public List<YgInformUser> find() {
		YgInformUserExample example = new YgInformUserExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygInformUserMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgInformUserExample example = new YgInformUserExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygInformUserMapper.countByExample(example);
	}

	@Override
	public YgInformUser findById(Long id) {
		return ygInformUserMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgInformUser ygInformUser) {
		return ygInformUserMapper.insertSelective(ygInformUser);
	}

	@Override
	public int update(YgInformUser ygInformUser) {
		return ygInformUserMapper.updateByPrimaryKeySelective(ygInformUser);
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgInformUserExample example = new YgInformUserExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgInformUser record = new YgInformUser();
		record.setStatus(DataStatus.Delete.name());
		return ygInformUserMapper.updateByExampleSelective(record, example);
	}
	
	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[]{id});
	}

	@Override
	public int saveOrUpdate(YgInformUser ygInformUser) {
		int r;
		if (ygInformUser.getId() != null) {
			r = this.update(ygInformUser);
		} else {
			r = this.save(ygInformUser);
		}
		return r;
	}

	@Override
	public ResultSet<YgInformUser> search(QueryInfo queryInfo, String q) {
		int count = ygInformUserMapper.countSuper(q);
		if (count == 0) {
			return new ResultSet<>();
		}
		List<YgInformUser> list = ygInformUserMapper.selectSuper(queryInfo, q);
		return new ResultSet<>(count,list);
	}

	@Override
	public UserInformModel findLatest(Long userId) {
		List<UserInformModel> informList = this.findInformList(new QueryInfo(1), userId, null);
		return CollectionUtils.isEmpty(informList) ? null : informList.get(0);
	}

	@Override
	public List<UserInformModel> findInformList(QueryInfo queryInfo, Long userId, Boolean readStatus) {
		return ygInformUserMapper.selectInformList(queryInfo, userId, readStatus);
	}

	@Override
	public int updateByReadAll(Long userId) {
		Assert.notNull(userId, "param userId can not be null");
		return ygInformUserMapper.updateByReadAll(userId);
	}

	@Override
	public List<UserInformModel> findByUserId(Long userId, InformType informType) {
		if (userId == null) {
			return null;
		}
		return ygInformUserMapper.selectByUserId(userId, informType);
	}
}

